package com.gg.goods;

import com.gg.entity.Fruit;
import com.gg.helper.DBHelper;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet(name = "GetFruitByFidServlet", value = "/goods/GetFruitByFidServlet")
public class GetFruitByFidServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*1.获取主键请求参数 f_id*/
        /*Integer.parseInt: 把字符串类型的数字"5" 转换成  整型变量 5*/
        Integer f_id = Integer.parseInt(request.getParameter("f_id"));
        /*2.SQL*/
        String sql = "select *  from  fruit where f_id = ?";
        /*3.CON*/
        Connection con = DBHelper.getConnection();
        /*4.发射器*/
        try {
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1, f_id);

            ResultSet rs = ps.executeQuery();
            rs.next();
            String f_name = rs.getString("f_name");
            String f_norm = rs.getString("f_norm");
            BigDecimal f_price = rs.getBigDecimal("f_price");
            String f_summary = rs.getString("f_summary");
            String f_prompt = rs.getString("f_prompt");
            int f_imgcount = rs.getInt("f_imgcount");
            String c_id = rs.getString("c_id");
            boolean f_entrance = rs.getBoolean("f_entrance");
            boolean f_hot = rs.getBoolean("f_hot");
            /*恢复这个查回来的单个水果*/
            Fruit fruit = new Fruit(f_id, f_name, f_norm, f_price, f_summary, f_prompt, f_imgcount, c_id, f_entrance, f_hot);
            /*把这个水果放Session中*/
            request.getSession().setAttribute("fruit", fruit);
            /*到fruit_info.jsp 显示去*/
            response.sendRedirect("/fruit/fruit_info.jsp");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


}
